<?php

	ob_clean();
	
	//新闻添加
	if(!empty($_POST)){

		// 获取POST数据
		$name = $_POST['name'];//标题
		$pid = $_POST['pid'];//类型

		//编写插入的sql语句
		$sql = "insert into `cate`(`name`,`pid`)values('$name','$pid')";

		// 执行sql语句
		mysqli_query($link,$sql);
		// 获取刚刚执行的添加语句受影响的行数（就是你插入的数据的记录总数）
		$_rows = mysqli_affected_rows($link);
		if($_rows>0){
				echo '<script>alert("数据添加成功");location.href="index.php?c=cate&a=list&admin=1";</script>';die;
	              
		}else{
			echo '<script>alert("数据添加失败");history.go(-1);</script>';die;
		}
	}

	$id = empty($_GET['id'])?0:$_GET['id'];
	//查询顶级分类
	$sql = "select * from `cate` where pid=$id";
	$result = mysqli_query($link,$sql);
	while($arr = mysqli_fetch_assoc($result)){
		$cate[] = $arr;
	}

//判断是否是ajax发送的请求
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])){

	echo json_encode($cate);die;
}

// 加载后台首页视图
include('protected/view/admin/layout.html');